package com.example.recite_words.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.example.recite_words.Bean.User;
import com.example.recite_words.MainActivity;
import com.example.recite_words.R;
import com.example.recite_words.util.Config;
import com.example.recite_words.util.Send_data;

import java.io.IOException;
import java.net.URLEncoder;

public class Login extends MainActivity {
    private JSONObject jsonObject = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Button log_login = findViewById(R.id.log_login);
        EditText id = findViewById(R.id.log_username);
        id.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        id.setText(MainActivity.TheUser.getId());
        id.setHorizontallyScrolling(true);
        EditText password = findViewById(R.id.log_password);
        password.setText(MainActivity.TheUser.getPassword());
        password.setHorizontallyScrolling(true);
        log_login.setOnClickListener(
                new View.OnClickListener() {
                    //内部匿名类
                    @Override
                    public void onClick(View view) {

                        String input_id = id.getText().toString();
                        String input_password = password.getText().toString();
                        //String input_affirm = affirm.getText().toString();
                        ///////////////数据传输
                        new Thread(() -> {
                            try {
                                String data = "Email=" + input_id
                                        +"&Passwd=" + URLEncoder.encode(input_password,"UTF-8");
                                jsonObject = Send_data.do_post(Login.this,data, Config.LOGIN_URL);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (jsonObject == null){
                                Toast.makeText(Login.this, "连接错误", Toast.LENGTH_SHORT).show();
                            }

                            else if ((int)jsonObject.get("code") == Config.login_success) {
                                runOnUiThread(() -> {
                                    Toast.makeText(Login.this, "登陆成功！", Toast.LENGTH_SHORT).show();
                                    MainActivity.TheUser = new User(jsonObject);
                                    new Thread(() -> {
                                        String data = "Email=" + MainActivity.TheUser.getId();
                                        //String filetype = Send_data.post_for_pic(data,Config.DOWNLOAD_FLIE,Login.this.getExternalFilesDir(null).getPath(),"user_icon");
                                        //MainActivity.TheUser.setIcon(Login.this.getExternalFilesDir(null).getPath() + "/" + "user_icon." + filetype);
                                        Intent main_intent = new Intent();
                                        main_intent.setClass(Login.this, Main.class);
                                        startActivity(main_intent);
                                        try {
                                             MainActivity.TheUser.WriteUser(Login.this);
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                    }}).start();

                                });
                            }
                            else if((int)jsonObject.get("code") == Config.already_login){
                                runOnUiThread(() -> {
                                    Toast.makeText(Login.this,"用户已在别处登陆", Toast.LENGTH_SHORT).show();
                                });
                            }
                            else {
                                runOnUiThread(() -> {
                                    Toast.makeText(Login.this,(String) jsonObject.get("message"), Toast.LENGTH_SHORT).show();
                                });
                            }
                            /*
                            else if((int)jsonObject.get("code") == Config.pass_error){
                                runOnUiThread(() -> {
                                    Toast.makeText(Login.this,"密码错误", Toast.LENGTH_SHORT).show();
                                });
                            }
                            else {
                                runOnUiThread(() -> {
                                    Toast.makeText(Login.this,"程序出错", Toast.LENGTH_SHORT).show();
                                });
                            }*/
                        }).start();

                    }
                });
        Button log_register = findViewById(R.id.log_register);
        log_register.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent register_intent = new Intent();
                        register_intent.setClass(Login.this, Register.class);
                        startActivity(register_intent);
                    }
                });
    }
}
